package it.webrules.deploymanager.core.condition;

import it.webrules.xsd.dmc.config.v1.BooleanOperator;
import it.webrules.xsd.dmc.config.v1.CombineCondition;
import it.webrules.xsd.dmc.config.v1.Condition;

public class ConditionsEngine {

	private final ConditionEngine conditionEngine;

	public ConditionsEngine(ConditionEngine conditionEngine) {
		this.conditionEngine = conditionEngine;
	}

	public boolean elabConditions(CombineCondition conditions) throws Exception {

		BooleanOperator boolop = conditions.getBoolop();

		for (CombineCondition combineCondition : conditions.getConditions()) {
			boolean out = elabConditions(combineCondition);

			if (boolop.equals(BooleanOperator.OR) && out)
				return true;

			if (boolop.equals(BooleanOperator.AND) && !out)
				return false;
		}

		for (Condition condition : conditions.getCondition()) {
			boolean out = conditionEngine.elabCondition(condition);

			if (boolop.equals(BooleanOperator.OR) && out)
				return true;

			if (boolop.equals(BooleanOperator.AND) && !out)
				return false;

		}

		return true;
	}

}
